package com.zhida.jiyangcenter.mian.fragment

import android.content.Context
import android.os.Bundle
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import chooongg.base.annotation.ContentLayout
import chooongg.base.utils.getStatusBarHeight
import chooongg.base.utils.gone
import chooongg.base.utils.startActivity
import chooongg.base.utils.visible
import com.zhida.jiyangcenter.R
import com.zhida.jiyangcenter.buycar.ShopCarActivity
import com.zhida.jiyangcenter.mian.BaseFragment
import kotlinx.android.synthetic.main.fragment_main_shop.*


@ContentLayout(R.layout.fragment_main_shop)
class MainShopFragment : BaseFragment() {
    override fun getTitle(): String {
        return "商城"
    }

    private val fragments = ArrayList<MainShopItemFragment>().apply {
        add(MainShopItemFragment().apply {
            setTitle("平台推荐")
        })
        add(MainShopItemFragment().apply {
            setTitle("营养膳食")
        })
        add(MainShopItemFragment().apply {
            setTitle("家居调理")
        })
        add(MainShopItemFragment().apply {
            setTitle("智能设备")
        })
    }
    private lateinit var adapter: Adapter

    private class Adapter(fm: FragmentManager, val fragments: ArrayList<MainShopItemFragment>) :
        FragmentPagerAdapter(fm) {
        override fun getItem(position: Int) = fragments[position]
        override fun getCount() = fragments.size
        override fun getPageTitle(position: Int) = fragments[position].getTitle()
    }

    override fun initConfig(savedInstanceState: Bundle?) {
        super.initConfig(savedInstanceState)
        view_status_bar.updateLayoutParams<ConstraintLayout.LayoutParams> {
            height = getStatusBarHeight()
        }
        adapter = Adapter(
            childFragmentManager,
            fragments
        )
        view_page_main_shop.adapter = adapter
        view_page_main_shop.offscreenPageLimit = fragments.size - 1
        tab_main_shop.setupWithViewPager(view_page_main_shop)
        iv_shop_buycar.setOnClickListener {
            startActivity<ShopCarActivity> {  }
        }
        iv_shop_search.setOnClickListener {
            ll_store_search.visible()
            iv_shop_search.gone()
        }
        edit_text.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                iv_shop_search.visible()
                ll_store_search.gone()
                val manager: InputMethodManager =
                    requireContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                manager.hideSoftInputFromWindow(
                    requireView().windowToken,
                    InputMethodManager.HIDE_NOT_ALWAYS
                )
                fragments[view_page_main_shop.currentItem].search(edit_text.text.toString())
            }
            false
        }
    }
}